package com.example.adminframework.model.bo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询参数
 */
@ApiModel("查询参数")
public class ParamsBO {
    /**
     * 查询列，为空时查询全部列
     */
    @ApiModelProperty("查询列。为空时查询全部列")
    private List<ColumnBO> columns;

    /**
     * 查询条件
     */
    @ApiModelProperty("查询条件")
    private List<FilterBO> filters;

    /**
     * 页码
     */
    @ApiModelProperty(value = "页码。分页查询时默认为1")
    private Integer currentPage;

    /**
     * 分组
     */
    @ApiModelProperty("分组")
    private List<GroupBO> groups;

    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页条数。分页查询时默认为20")
    private Integer pageSize;

    /**
     * 排序
     */
    @ApiModelProperty("排序")
    private List<SortBO> sorts;



    public ParamsBO column(String... columns) {
        List<ColumnBO> columnList = Arrays.stream(columns).map(ColumnBO::new).collect(Collectors.toList());
        return columns(columnList);
    }

    public ParamsBO columns(List<ColumnBO> columns) {
        if (this.columns == null) {
            this.columns = new ArrayList<>();
        }

        this.columns.addAll(columns);
        return this;
    }

    public ParamsBO filter(String field, String operator, Object value) {
        return filters(Collections.singletonList(new FilterBO(field, operator, value)));
    }

    public ParamsBO filters(List<FilterBO> filters) {
        if (this.filters == null) {
            this.filters = new ArrayList<>();
        }

        this.filters.addAll(filters);
        return this;
    }

    public ParamsBO group(String... groups) {
        List<GroupBO> groupList = Arrays.stream(groups).map(GroupBO::new).collect(Collectors.toList());
        return groups(groupList);
    }

    public ParamsBO groups(List<GroupBO> groups) {
        if (this.groups == null) {
            this.groups = new ArrayList<>();
        }

        this.groups.addAll(groups);
        return this;
    }

    public void setColumns(List<ColumnBO> columns) {
        this.columns = columns;
    }

    public void setFilters(List<FilterBO> filters) {
        this.filters = filters;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public void setGroups(List<GroupBO> groups) {
        this.groups = groups;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setSorts(List<SortBO> sorts) {
        this.sorts = sorts;
    }

    public ParamsBO sort(String field, String order) {
        return sorts(Collections.singletonList(new SortBO(field, order)));
    }

    public ParamsBO sorts(List<SortBO> sorts) {
        if (this.sorts == null) {
            this.sorts = new ArrayList<>();
        }

        this.sorts.addAll(sorts);
        return this;
    }

    public ParamsBO currentPage(Integer currentPage) {
        this.currentPage = currentPage;
        return this;
    }

    public ParamsBO pageSize(Integer pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public List<ColumnBO> getColumns() {
        return columns;
    }

    public List<FilterBO> getFilters() {
        return filters;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public List<GroupBO> getGroups() {
        return groups;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public List<SortBO> getSorts() {
        return sorts;
    }

    /**
     * 普通分组
     */
    public List<GroupBO> getGroupList() {
        if (groups == null) {
            return null;
        }
        return groups.stream().filter(item -> StringUtils.isBlank(item.getTimeUnit()) || item.getTimeValue() == null).collect(Collectors.toList());
    }

    /**
     * 时间分段统计
     */
    public GroupBO getTimeInterval() {
        if (groups == null) {
            return null;
        }
        return groups.stream().filter(item -> StringUtils.isNotBlank(item.getTimeUnit()) && item.getTimeValue() != null).findAny().orElse(null);
    }

    @Override
    public String toString() {
        return "ParamsBO{" +
                "columns=" + columns +
                ", filters=" + filters +
                ", currentPage=" + currentPage +
                ", groups=" + groups +
                ", pageSize=" + pageSize +
                ", sort=" + sorts +
                '}';
    }

    /**
     * 序列化
     */
    public void serialization() {

    }
}
